
	
	cd "$mydata"


	use  datacontrol_panel , clear		
	
	
	
	/* POINT 1: TAKE CONTROL VARIABLES NEEDED IN 2005 */ 

		
		* lived in US
		tempvar t
		bys pid_link : egen `t'= max((year==2002) * (livedinUS ==1))
		replace livedinUS =1 if `t'==1 & year==2005
		* new member in hh that has lived in U.S
		g returnees_US = (statusliving05==4)*(livedinUS ==1)
		bys folio: egen hh_returnmigUS_0502 = max(  (statusliving05==4)*(livedinUS ==1))
		
		bys pid_link : egen  earnings_month_p02=  max((year==2002) * earnings_month_p ) 
		bys pid_link : egen  benef2000=  max((year==2002) *  benefityear2000  ) 
		
		tempvar t t2
		  g `t'= yedu if year==2002
		  bys pid_link : egen yedu02= max(`t')
		  g `t2'= yedu if year==2005
		 bys pid_link :  egen yedu05= max(`t2')
		  
		
		* new and old hh
		*bys folio: egen inityear=min(year) 
		
		
		bys folio: egen id_mun02 = max( id_mun*(year==2002))
		bys folio: egen id_mun05 = max( id_mun*(year==2005))
		
		  tempvar t t2
		  g `t'= book2 if year==2005
		  g `t2'= book2 if year==2002
		  bys folio: egen book2_02= max(`t2')
		  bys folio: egen book2_05= max(`t')
	
	
	
	bys year folio: egen nb55plus_male = total( age>=55 & male==1)
	bys year folio: egen  nb55plus_female  = total( age>=55 & female==1)
	
	foreach var in nbage6less  nbage713  nbmaleage1435  nbfemaleage1435 nbmaleage3654  nbfemaleage3654  nbage55plus ///
					nb55plus_female nb55plus_male   ln_socialnonlabincome{
	
			g v02= `var' if year==2002
			g v05= `var' if year==2005
			
			bys folio: egen tmp02 = max(v02)
			bys folio: egen tmp05 = max(v05)
			
			g `var'_0502 = tmp05 - tmp02
			
			local labvariable :  variable label `var'
			label var `var'_0502  "`labvariable' 0502 "
			
			drop v02 v05 tmp02 tmp05
		}
		*

	foreach i in age6less  age713  maleage1435  femaleage1435 maleage3654  femaleage3654  age55plus 55plus_female 55plus_male {
		
		g ARRIVE_`i'  = nb`i'_0502   > 0 & nb`i'_0502 !=.
		g LEAVE_`i'   = nb`i'_0502  < 0 & nb`i'_0502 !=.
		}
		*
		
		bys folio: egen hh_newmem = max(statusliving05 ==4)	
		egen hh_newmem_adu = rowtotal(ARRIVE_maleage1435 ARRIVE_femaleage1435  ARRIVE_femaleage3654  ) 
		replace hh_newmem_adu= (hh_newmem_adu>0) 
		

	
	*HH Shocks 
		
		replace loss_naturaldis_0205  = 1 if robbery_0205==1 
		replace loss_naturaldis_0298  = 1 if robbery_0298==1 
		rename loss_naturaldis_0205 robb_naturaldis_0205
		rename loss_naturaldis_0298 robb_naturaldis_0298
	
		global s0205  dead_0205 disease_0205 job_busi_failure_0205 loss_crop_0205 robb_naturaldis_0205  
		global s0298 dead_0298 disease_0298 job_busi_failure_0298 loss_crop_0298 robb_naturaldis_0298   

		foreach v in $s0205 $s0298 {
		replace `v'= . if book2_02==0 | book2_05==0
		}
*

		label var dead_0205 "death hh member betw. 2002-2005"
	    label var dead_0298 "death hh member betw. 1998-2002"
		
		label var  disease_0205 "Illness hh member betw. 2002-2005"
		label var  disease_0298 "Illness hh member betw. 1998-2002"
		
		label var   job_busi_failure_0205 "Job loss or Business failure betw. 2002-2005"
		label var   job_busi_failure_0298 "Job loss or Business failure betw. 1998-2002"
		
		label var  loss_crop_0205 "Crop Loss  betw. 2002-2005" 
		label var   loss_crop_0298 "Crop Loss  betw. 1998-2002"
		
		label var  robb_naturaldis_0205  "House\business\animals loss betw. 2002-2005" 
		label var  robb_naturaldis_0298  "House\business\animals loss betw. 1998-2002" 
		
		


*************************************
	
	
		
		keep if year==2002
		
		
		merge 1:1 folio ls year using outcomes_data02  
		drop if _m==2
		drop _m
		
		merge m:1 folio year using  HH_outcomes_data02
		drop if  _m==2 
		drop _m
		
		
		merge m:1 folio year using HH_migrationstatus_US 
		drop if _m==2 
		drop  _m
		
		merge 1:1 folio ls year using IND_migrationstatus_US
		drop if  _m==2 
		drop _m
		
	
		drop *emif*
		drop *uwmex*
		drop *Closest*
		
		
        replace ruralarea= community_type==4 | community_type==3
		bys folio: egen ruralarea02 = max( ruralarea ==1 & year==2002)
		


/*stat desc */


		
/* POINT 1 A :  Treatment   */ 
	
	* Treatment
	
			g treatment = hhusmigrant ==1 /*| hhmexmigrant==1*/ 
			label var treatment  "US mig"
			label var  hhusmigrant "US mig"
			
			*replace  hhmexmigrant=0
			
			
			label var hhmexmigrant "Mex mig"
			rename  hhnucfamUS  hhnucfamUS02 
			label var hhnucfamUS02  "us network 02" 
			
			
			bys folio: egen hh_leave1260= max(statusliving05==3 & age>12 & age<60 ) 
			
			g hhdecohabit =  hh_leave1260 &  hhtypemigrant ==3 
			label var hhdecohabit "exo hhsplit up"
			replace treatment=0 if    hh_leave1260==0
			replace  hhmexmigrant=0 if  hh_leave1260==0
			
			bys folio: egen hh_depar_farmer = max(statusliving05 ==3  & agriactivity ==1 & age>11  )
			bys folio: egen hh_depar_self = max(statusliving05 ==3  & self ==1 & age>11  )
			bys folio: egen hh_depar      = max(statusliving05 ==3  & age>11  )
			
			bys folio: egen hh_work = max( lastyear_work ) 
			bys folio: egen hh_self = max( self ==1)
			bys folio: egen hh_farm = max(  agriactivity ==1)
			bys folio: egen hh_wagearner = max(  noagrw ==1 | agrw==1)
			

			
			bys folio: egen hh_depar_worker      = max(statusliving05 ==3  &  status ==1  & mwp ==1 )

		
			 bys  folio: egen  HH_nb_stay   =   total(  statusliving05==1 )	
	
	* other indep variable. 
	
			
		macro drop  ind*		
		g inmigration_14more_fem   =   ARRIVE_femaleage1435 + ARRIVE_femaleage3654 + ARRIVE_55plus_female > 0 
		g inmigration_14more_male  =     ARRIVE_maleage1435   +   ARRIVE_maleage3654  +   ARRIVE_55plus_male > 0 
		label var inmigration_14more_fem  "in migration female 14+"
		label var  inmigration_14more_male "in migration male 14+"

		replace hhreceive =0 if   HH_transfer02==0

		g age_female= age*female 
		
		
		
	cap drop child old 
	g child = age <=13 & age!=.  & age >=3 

	replace agriactivity05= 0 if agriactivity05==3
	replace agriactivity0502 = agriactivity05  - agriactivity02
		
	
**** HOUSEHOLD LEVEL OUTCOMES ****
	
		bys folio : egen HHnomig_trans02 = total( IND_moneyreceive02)
		bys folio : egen HHnomig_trans05 = total( IND_moneyreceive05)
		g HHnomig_trans0502 = HHnomig_trans05 - HHnomig_trans02
		
		
		label var HHnomig_trans0502  "tot trans nonmigr 0502"
		
		
		g ind_rec02 = IND_moneyreceive02>0 if IND_moneyreceive02!=. 
		g ind_rec05 = IND_moneyreceive05>0 if IND_moneyreceive05!=. 
		g ind_rec0502 = ind_rec05 -   ind_rec02 
		label var  ind_rec0502 "binary ind receive 0502"
		
	
	label var IND_moneyreceive0502 "  IND_moneyreceive0502 "
	label var IND_moneyreceive0902 "  IND_moneyreceive0902 "

	label var ind_giftreceive0502 "ind gift received0502"
	label var hh_giftreceive0502 "HH gift received0502"
	
	
	tempvar t1 t2
	bys folio: egen `t1' = mean( IND_moneyreceive02	) /* average among non-migrants permanent memb*/
	bys folio: egen `t2' = mean( IND_moneyreceive05	) /* average among non-migrants permanent memb */
	g HH_average_receipt0502 = `t2'- `t1'
	label var HH_average_receipt0502  "HH av. transfer 05-02"
	
	tempvar t3
	g `t3' = IND_moneyreceive0502 if rel_head==1 /* change in transfers received bu HH head : missing when head migrates*/ 
	bys folio: egen HH_head_receipt0502 = mean(`t3') 
	label var HH_head_receipt0502  "HH head transfer 05-02"
	
	g head_receipt0502 = IND_moneyreceive0502 if rel_head==1 
	g head_pecap_receipt_0502 = IND_moneyreceive05/(HH_nbsize05) - IND_moneyreceive02/(HH_nbsize02) if rel_head==1 
	label var head_receipt0502  " head_receipt0502"
	label var head_pecap_receipt_0502 " head percap receipt0502"
	
	g head_receipt0902 = IND_moneyreceive0902 if rel_head==1 
	g head_pecap_receipt_0902 = IND_moneyreceive09/(HH_nbsize09) - IND_moneyreceive02/(HH_nbsize02) if rel_head==1 
	label var head_receipt0902  " head_receipt0902"
	label var head_pecap_receipt_0902 " head percap receipt0902"
	
	
	
	foreach y in 02 05 09 0502 {
 	
	replace HH_constot`y'= . if HH_foodcons`y'==. | HH_nonfood`y' ==.
	}
	
	g UnearnedINC02 = (HH_constot02 - HH_labinc_month02 ) 
	g UnearnedINC0502 = (HH_constot0502 - HH_labinc_month0502 ) 
	
	g UnearnedINC0902 = ( HH_constot0902 - HH_labinc_month0902 )
	
	
	g cons_precap_0502=  HH_constot05/(HH_nbsize05) - HH_constot02/(HH_nbsize02)
	g cons_precap_0902=  HH_constot09/(HH_nbsize09) - HH_constot02/(HH_nbsize02)
	
	 g food_attana_02 = HH_foodcons02/HH_attasio_scale02  
	 g nonfood_attana_02  = HH_nonfood02/HH_attasio_scale02 
	 g cons_attana_02   = HH_constot02/HH_attasio_scale02 


	g cons_rojas_0502  = HH_constot05/(HH_nbsize05)^0.76 - HH_constot02/(HH_nbsize02)^0.76
	g cons_attana_0502 = HH_constot05/HH_attasio_scale05  - HH_constot02/HH_attasio_scale02 
	g cons_rojas_0902  = HH_constot09/(HH_nbsize09)^0.76 - HH_constot02/(HH_nbsize02)^0.76
	g cons_attana_0902 = HH_constot09/HH_attasio_scale09  - HH_constot02/HH_attasio_scale02 
	
	g food_rojas_0502  = HH_foodcons05/(HH_nbsize05)^0.76 - HH_foodcons02/(HH_nbsize02)^0.76
	g food_attana_0502 = HH_foodcons05/HH_attasio_scale05  - HH_foodcons02/HH_attasio_scale02 
	g food_attana_0902 = HH_foodcons09/HH_attasio_scale09  - HH_foodcons02/HH_attasio_scale02 

	g nonfood_rojas_0502  = HH_nonfood05/(HH_nbsize05)^0.76 -  HH_nonfood02/(HH_nbsize02)^0.76
	g nonfood_attana_0502 = HH_nonfood05/HH_attasio_scale05  -  HH_nonfood02/HH_attasio_scale02 
	g nonfood_attana_0902 = HH_nonfood09/HH_attasio_scale09  -  HH_nonfood02/HH_attasio_scale02 
	
	
	local i =1
	foreach T in 0.3 0.5 0.7  0.9  {
	g cons_theta`i'_02= HH_constot02/(HH_nbsize02)^`T'
	g cons_theta`i'_0502=  HH_constot05/(HH_nbsize05)^`T' - HH_constot02/(HH_nbsize02)^`T'
	g cons_theta`i'_0902=  HH_constot09/(HH_nbsize09)^`T' - HH_constot02/(HH_nbsize02)^`T'
	label var cons_theta`i'_0502 "cons scale `T' 0502" 
	label var cons_theta`i'_0902 "cons scale `T' 0902" 
	local i = `i'+1
	}
	global consdetails cons_theta1_0502 cons_theta2_0502 cons_theta3_0502 cons_theta4_0502 cons_theta1_0902 cons_theta2_0902 cons_theta3_0902 cons_theta4_0902

	
	g HH_hasruralcost0502 = ( HH_ruralcost05>0 )   - ( HH_ruralcost02>0 ) if HH_ruralcost05!= .& HH_ruralcost02!=. 
	g HH_soil0502 = ( HH_soil_input05>0 )   - ( HH_soil_input02>0 ) if HH_soil_input05!= .& HH_soil_input02!=. 

			
	foreach v in  y_m y_f a_m a_f  o_f o_m A_a { 
	cap drop `v'
	}
	*
	*

	

	g HH_one_outworker_business02= HH_nb_outwork_business02>0 if HH_nb_outwork_business02 !=.
	g HH_one_outworker_business05= HH_nb_outwork_business05>0 if HH_nb_outwork_business05 !=.
	g HH_one_outworker_land02    = HH_nb_outworkers_land02 >0 if HH_nb_outworkers_land02 !=.
	g HH_one_outworker_land05    = HH_nb_outworkers_land05 >0 if HH_nb_outworkers_land05 !=.
	
	g HH_one_outworker_land0502 =  HH_one_outworker_land05 -HH_one_outworker_land02
	g HH_one_outworker_business0502= HH_one_outworker_business05- HH_one_outworker_business02
	
	replace  HH_nb_outworkers_land02= min(  HH_nb_outworkers_land02,4) if  HH_nb_outworkers_land02!=.
	replace  HH_nb_outworkers_land05= min(  HH_nb_outworkers_land05,4) if  HH_nb_outworkers_land05!=.
	replace  HH_nb_outworkers_land0502=  HH_nb_outworkers_land05 -  HH_nb_outworkers_land02
	
	replace  HH_nb_outwork_business02= min(  HH_nb_outwork_business02,4) if  HH_nb_outwork_business02!=.
	replace  HH_nb_outwork_business05= min(  HH_nb_outwork_business05,4) if  HH_nb_outwork_business05!=.
	replace  HH_nb_outwork_business0502=  HH_nb_outwork_business05 -  HH_nb_outwork_business02
	

		
		
		
foreach y in 0502 0902 {
label var HH_cons_perhead`y' "HHcons cap `y'"
label var  cons_rojas_`y'  "HHcons rojascale `y'"
label var cons_attana_`y'  "HHcons attanas `y'"
label var cons_precap_`y' "HHcons cap `y'"
}
*

label var workLW_mwp0502 "wage work"
label var hourLW_mwp0502 "wage work"

label var  workLW0502         "Any work"
label var workLW_noagrw0502   "Non ag. Wage work"
label var  workLW_agrw0502        "Ag. wage work"
label var workLW_self0502  	  "Self-employed work"
label var  agriactivity0502   " any farm "
label var	hourLW0502   			"Any work"
label var	hourLW_noagrw0502  		"Non ag. Wage work"
label var	hourLW_agrw0502    		 "Ag. wage work"
label var	hourLW_self0502 		"Self-employed work"
label var	housework0502           "household chores"
label var care_elderchild0502  "caring children and elderly" 
label var hoursagriactivity0502 	   "Any farm"	
label var stillinschool0502   	   "still in school"
label var bad_nutrition0502    "Bad nutrition"
label var bad_self_health0502   "Bad self health" 
label var nb_symptom0502   "Nb symptom"
label var calderon_depress0502   " calderon_depress" 
label var UnearnedINC0502 "Unearned inc"  
label var HH_incomemonth_mwp0502  "hh earnings (month)"
label var HH_incomemonth_self0502 "hh profits"
label var HH_labinc_month0502 "hh laborinc"
label var HH_incomeyear_mwp0502   "hh earnings (year)"
label var HH_incomeyear_self0502  "hh profits"
label var HH_labincome0502  "hh labinc"
label var HH_transfer0502   "hh private transfer"
label var HH_socialtrans0502  "hh social transfer"
label var HH_foodcons0502  "total food cons"
label var HH_nonfood0502 "total non food cons"
label var HH_constot0502  "total consumption" 
label var HH_foodcons_cap0502 "Food  per cap"
label var HH_nonfood_cap0502   "NonFood cons per cap"
label var HH_cons_cap0502  "Cons per cap"
label var ln_HH_foodcons0502  "ln food cons"
label var ln_HH_nonfood0502 "ln nofood cons"
label var ln_HH_constot0502  "ln cons" 
label var ln_HH_foodcons_cap0502 " ln Food percap"
label var ln_HH_nonfood_cap0502   "ln NoFood percap"
label var ln_HH_cons_cap0502  "ln Cons percap"
label var HH_workLW_self0502  "hh self"
label var  HH_hourLW_self0502  "hh hour self" 
label var HH_workLW_noagrw0502   "hh noagr"
label var HH_hourLW_noagrw0502  "hh hour noagr"
label var   HH_workLW_agrw0502  "hh agrw"
label var  HH_hourLW_agrw0502  "hh hou agrw"


label var   HH_hoursagriactivity0502 "hh agri time" 

 label var HH_nbsize0902 "hh size 2009-2002"
 label var HH_nbadult0902 "nb adu 2009-2002"

 

	****************************


	
	g urban = ruralarea==0
	g rural = ruralarea==1
	

	foreach y in 02 05 09 {
	
g farm_prod_ph_`y' = HH_incomemonth_self`y'/HH_hourLW_self`y' if HH_incomemonth_self`y'>0 
g farm_prod_pw_`y' = HH_incomemonth_self`y'/HH_workLW_self`y' if HH_incomemonth_self`y'>0
g farm_incap_`y'  = HH_incomemonth_self`y'/HH_nbsize`y'

*
replace farm_incap_`y'=. if HH_incomemonth_self`y'==0 & HH_workLW_self`y'>0 & HH_workLW_self`y'!=.
}

g farm_prod_ph_0502= farm_prod_ph_05-farm_prod_ph_02
g farm_prod_pw_0502 = farm_prod_ph_05-farm_prod_ph_02
g farm_incap_0502 = farm_prod_ph_05-farm_prod_ph_02

g farm_prod_ph_0902= farm_prod_ph_09-farm_prod_ph_02
g farm_prod_pw_0902 = farm_prod_ph_09-farm_prod_ph_02
g farm_incap_0902 = farm_prod_ph_09-farm_prod_ph_02

g HH_allfarmsales02 = HH_livestockincome02 + HH_cropsales02 + HH_ruralsales02 
g HH_allfarmsales05 = HH_livestockincome05 + HH_cropsales05 + HH_ruralsales05 

g HH_POS_allfarmsales02 = HH_POS_livestockincome02 + HH_POS_cropsales02 + HH_POS_ruralsales02 
g HH_POS_allfarmsales05 = HH_POS_livestockincome05 + HH_POS_cropsales05 + HH_POS_ruralsales05 
replace  HH_POS_allfarmsales02 = min( HH_POS_allfarmsales02,1) 
replace  HH_POS_allfarmsales05 = min( HH_POS_allfarmsales05,1) 

g  HH_allfarmsales0502=  HH_allfarmsales05 -  HH_allfarmsales02 
g  HH_POS_allfarmsales0502=  HH_POS_allfarmsales05 -  HH_POS_allfarmsales02 


foreach v in    fertilizer manure pesticidies seeds tractorent yoke workerwage water fuel	 {
				
				replace HH_`v'02 = HH_`v'02 >0 if  HH_`v'02!=.
				replace HH_`v'05 = HH_`v'05 >0  if  HH_`v'05!=.
				replace HH_`v'09 = HH_`v'09 >0   if  HH_`v'09!=.
				
				replace HH_`v'0902 =  HH_`v'09 -  HH_`v'02
				replace HH_`v'0502 =   HH_`v'05-   HH_`v'02
			
			}
			*
foreach y in 02 05 09 {

g HH_seedpest`y' =  ( HH_pesticidies`y' + HH_seeds`y'  >0) if HH_pesticidies`y' !=. & HH_seeds`y' !=. 
label var HH_seedpest`y' "HH has improved seeds or pesticides `y'"
}
*
g HH_seedpest0502 =  HH_seedpest05 -  HH_seedpest02
g HH_seedpest0902 =  HH_seedpest09 -  HH_seedpest02
label var HH_seedpest0502  "HH has improved seeds  or pesticides 05-02"
label var HH_seedpest0902  "HH has improved seeds or pesticides 05-02"

label var HH_fertilizer0502 "hh has fertilizers 05-02"
label var HH_fertilizer0902  "hh has fertilizers 09-02"			
			

			
*** KEEP ONLY VARIABLES THAT ARE USED IN THE ANALYSIS
	
#delimit ; 	
	keep 
treatment folio ls year pid_link statusliving05  female male  age age_female   yedu*  single   ln_socialnonlabincome   						  
hh_higestgrade3  hh_higestgrade4  book2*   hhusmigrant usmigrant
wealthindex  wealthindexsq  
hhsize      nbage13minus nbmaleage3654 nbmaleage1435
hhrelativeUS hhreturnees
ARRIVE_age6less    
LEAVE_age6less 
dead_0205 disease_0205 job_busi_failure_0205 loss_crop_0205 robb_naturaldis_0205  
dead_0298  disease_0298 job_busi_failure_0298 loss_crop_0298 robb_naturaldis_0298 
hhmexmigrant   hh_newmem_adu  statusliving05 hhpotentialmigration
rural community_type hourLW02   workLW02  hh_self  hh_farm 
hh_depar_self  hh_depar_worker  hh_depar_farmer 
UnearnedINC0502 food_attana_0502 nonfood_attana_0502  cons_attana_0502      
HH_ownfinancialassets0502	HH_ownbicycle0502 	HH_ownmotorvehic0502 	
HH_ownelecdevice0502 	HH_ownwashingmachine0502 HH_owndomesappliance0502
HH_own_draftanimal0502   HH_own_productanimal0502 HH_ownequipement0502 			
HH_POS_soil_input0502     HH_POS_otherland_input0502  
HH_POS_soil_input0902     HH_POS_otherland_input0902  
HH_soil_input0902     	 HH_otherland_input0902 	
cons_precap_0902    cons_rojas_0902 food_attana_0902 nonfood_attana_0902  cons_attana_0902   HH_nbsize0902 HH_nbadult0902     
farm_prod_ph_0502 farm_prod_pw_0502 farm_incap_0502 farm_prod_ph_0902 farm_prod_pw_0902 farm_incap_0902 
HH_incomemonth_self0502 HH_incomemonth_self0902 
hh_newmem_adu HH_work* HH_hour* HH_agri* con* HH_own* UnearnedINC* food_attana_* nonfood_attana_* HH_POS_soil_*  HH_POS_otherland*   HH_fertilizer*   HH_seedpest*  
rural community_type hourLW02   workLW02  hh_self  hh_farm  
treatment     hhmexmigrant    HH_nb_outwork_business*  HH_nb_outworkers_land*  HH_one_outworker_land*  HH_one_outworker_business* 
id_mun   nbmaleage3654 nbmaleage1435 nbage55plus   farm_*  HH_incomemonth_self*  HH_*livestockincome*  HH_*cropsales* HH_*ruralsales* HH_*allfarm* 
   workLW*   workLW_noagrw*      workLW_agrw*    workLW_self*   agriactivity*   
hourLW*     hourLW_noagrw*   hourLW_agrw*     hourLW_self*   hoursagriactivity* 
		housework*  	  care_elderchild*   stillinschool*    worklastyear*
 ind_rec*    INC_MONTH*  attrited_hh ruralarea rel_head  spouse head IND_moneyreceive* HH_attasio_scale* HH_constot* HH_foodcons* HH_nonfood*  HH_labinc_month* HH_socialtrans*;
		
	#delimit cr;
	
			
 save MxFLS_final, replace 
 
 
 
 
